package com.org.util;

import com.org.constant.Params;
import com.org.constant.ResponseEnum;
import com.org.exception.RespException;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.apache.shiro.crypto.hash.SimpleHash;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;
import java.util.UUID;

/**
 * author: vane.Q
 * date: 2022/10/19 14:53
 */
public class Utils {
    /**
     *
     * @param password 要加密的密码
     * @param saltValue 盐值
     * @return
     */
    public static String encrypt(String password,String saltValue){
        Object result;
        try {
            //创建一个MD5的盐值对象
            Object salt = new Md5Hash(saltValue);
            //生成加密的字符串
            result = new SimpleHash("MD5", password, salt, 1024);

        } catch (Exception ex) {
            throw new RespException(ResponseEnum.ERR_UTILS_ENCRYPT_ERROR);
        }
        return result+"";
    }

    public static String getTriggerName() {
        return UUID.randomUUID().toString().replace("-","");
    }

    public static String getCurrentDateStr(Date date) {
        SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return fmt.format(date);
    }

    public static String getUserId() {
        return UUID.randomUUID().toString().replace("-","");
    }

    public static String getSalt() {
        return String.valueOf(System.currentTimeMillis() + new Random().nextInt(1000)).substring(5);
    }

    public static String getTokenKey(String userId) {
        return Params.TOKEN + userId;
    }

}

